package com.example.beijing.beijingdemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.beijing.beijingdemo.R;
import com.example.beijing.beijingdemo.adapter.MenuAdapter;
import com.example.beijing.beijingdemo.adapter.TabAdapter;
import com.example.beijing.beijingdemo.base.BaseFragment;
import com.example.beijing.beijingdemo.base.BaseLoadNetDataOperator;
import com.example.beijing.beijingdemo.bean.NewsCenterBean;
import com.example.beijing.beijingdemo.fragment.GovaffairsTabFragment;
import com.example.beijing.beijingdemo.fragment.HomeTabFragment;
import com.example.beijing.beijingdemo.fragment.NewscenterTabFragment;
import com.example.beijing.beijingdemo.fragment.SettingTabFragment;
import com.example.beijing.beijingdemo.fragment.SmartserviceTabFragment;
import com.example.beijing.beijingdemo.view.NoScrollViewPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private List<Fragment> fragments;

    private NoScrollViewPager mVp;
    private RadioGroup rg_tab;
    public SlidingMenu slidingMenu;

    // 侧滑菜单的数据
    private List<NewsCenterBean.NewsCenterMenuBean> mNewsCenterMenuBeanList;
    private MenuAdapter mMenuAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initViewPager();
        initSlidingMenu();

        // 初始侧滑Menu菜单
        initMenuRecyclerView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mVp = (NoScrollViewPager) findViewById(R.id.vp);
        rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
        // 设置选择监听
        rg_tab.setOnCheckedChangeListener(this);
    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(new HomeTabFragment());
        fragments.add(new NewscenterTabFragment());
        fragments.add(new SmartserviceTabFragment());
        fragments.add(new GovaffairsTabFragment());
        fragments.add(new SettingTabFragment());
        mVp.setAdapter(new TabAdapter(getSupportFragmentManager(), fragments));

        // ViewPager滑动监听
        mVp.addOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // ViewPager切换到对应的页面
        int item = 0;
        switch (checkedId) {
            case R.id.rb_home:
                item = 0;
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                break;
            case R.id.rb_newscenter:
                item = 1;
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                break;
            case R.id.rb_smartservice:
                item = 2;
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                break;
            case R.id.rb_govaffairs:
                item = 3;
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                break;
            case R.id.rb_setting:
                item = 4;
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                break;
        }
        mVp.setCurrentItem(item, false);// ViewPager切换的时候,不需要滑动效果

        // 获取到对应的fragment页面
        BaseFragment fragment = (BaseFragment) fragments.get(item);
        if (fragment instanceof BaseLoadNetDataOperator) {
            BaseLoadNetDataOperator operator = (BaseLoadNetDataOperator) fragment;
            operator.loadNetData();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        // ViewPager滑动监听
        RadioButton buttonChild = (RadioButton) rg_tab.getChildAt(position);
        buttonChild.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 初始化SlidingMenu侧滑菜单
     */
    private void initSlidingMenu() {
        // 创建SlidingMenu菜单
        slidingMenu = new SlidingMenu(this);
        // 设置菜单左边滑动
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 设置全屏可以滑出
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        // 设置侧滑的宽度,200px
        slidingMenu.setBehindWidth(300);
        // 关联Activity,以主界面content的形式
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        // 设置菜单的布局
        slidingMenu.setMenu(R.layout.menu);
    }

    /**
     * 初始化侧滑Menu菜单
     */
    private void initMenuRecyclerView() {
        RecyclerView mRecycleView = (RecyclerView) slidingMenu.findViewById(R.id.menu_recyclerview);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);

        // 创建数据适配器给RecycleView进行绑定
        mMenuAdapter = new MenuAdapter(this, null);
        mRecycleView.setAdapter(mMenuAdapter);
    }

    /**
     * 让NewsCenterFragment设置数据
     */
    public void setData(List<NewsCenterBean.NewsCenterMenuBean> list) {
        this.mNewsCenterMenuBeanList = list;
        mMenuAdapter.setmDataList(list);
        mMenuAdapter.notifyDataSetChanged();
    }

    /**
     * 获取当前选中的Fragment
     */
    public BaseFragment getCurrentTabFragment() {
        int currentItem = mVp.getCurrentItem();
        BaseFragment fragment = (BaseFragment) fragments.get(currentItem);
        return fragment;
    }
}
